package cn.dengta.webapp.trade.entity;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

import cn.dengta.common.model.Status;
import cn.dengta.common.util.Formats;
import cn.dengta.context.model.Member;
import cn.dengta.context.model.WesKit;
import cn.dengta.webapp.base.entity.TimeBasedEntity;
import cn.dengta.webapp.trade.form.PayerMaxReqForm;
import cn.dengta.webapp.trade.model.*;
import cn.dengta.webapp.user.model.Gender;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.handlers.FastjsonTypeHandler;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import me.codeplayer.util.CollectionUtil;
import me.codeplayer.util.X;

/**
 * 积分提现记录表
 *
 * @author yuqidi
 * @since 2022-03-14
 */
@Setter
@Getter
@Accessors(chain = true)
@TableName(value = "tr_cash_log", autoResultMap = true)
public class CashLog extends TimeBasedEntity implements AuditScoped {
	/** 分包处理 */
	private WesKit wesKit;
	/** 用户ID */
	private Long userId;
	/** 国家码 */
	private String country;
	/** 关联 CashAccount ID */
	private Long accountId;
	/** 提现币种 */
	private String currency;
	/** 提现积分 */
	private BigDecimal integral;
	/** 提现金额 */
	private BigDecimal amount;
	/** 提现美金 */
	private BigDecimal amountDollar;
	/** 支付平台 */
	private Integer platform;
	/**
	 * 提现状态
	 *
	 * @see cn.dengta.webapp.trade.model.CashStatus
	 */
	private Integer status;
	/** 第三方支付平台的提现订单号 */
	protected String orderNo;
	/** 备注。在 美事通提现接口中，用于保存服务主体ID */
	private String remark;

	/** 退款金额（在主播提现过程中，如果收益对应的消费者进行了退款，则需要记录退款金额） */
	private BigDecimal refundAmount;
	/** 服务费 */
	private BigDecimal fee;
	/** 实到金额（=提现金额-服务费-坏账金额） */
	private BigDecimal realAmount;
	/** 实到美金 */
	private BigDecimal realDollar;
	/** 坏账 */
	private BigDecimal badDebt;
	// 审核
	/** 审核人 */
	protected Long verifyUserId;
	/** 审核备注 */
	protected String verifyRemark;
	/** 审核时间 */
	protected Date verifyTime;
	/** 复审人ID */
	protected Long reviewUserId;
	/** 复审备注 */
	protected String reviewRemark;
	/** 复审时间 */
	protected Date reviewTime;
	/** 第三方订单号 */
	protected String thirdOrderNo;
	/** 支付渠道商户别名 */
	public String alias;
	/** 核查人ID */
	protected Long checkUserId;
	/** 核查备注 */
	protected String checkRemark;
	/** 核查时间 */
	protected Date checkTime;
	/** 核查状态 */
	protected Integer checkStatus;

	protected Long configId;

	/** 账号数据 */
	@TableField(typeHandler = FastjsonTypeHandler.class)
	private CashAccount accountInfo;
	/** 付款结果信息 */
	@TableField(typeHandler = FastjsonTypeHandler.class)
	private JSONObject payResInfo;
	/** 付款请求数据 */
	@TableField(typeHandler = FastjsonTypeHandler.class)
	private PayerMaxReqForm payReqInfo;
	/** 付款请求结果 */
	@TableField(typeHandler = FastjsonTypeHandler.class)
	private JSONObject paymentResult;
	/** 性别 */
	public transient Gender gender;
	/** 昵称 */
	public transient String nickname;

	private transient int counter;
	protected transient CashAccount cashAccount;

	public static final String USER_ID = "user_id";
	public static final String ACCOUNT_ID = "account_id";
	public static final String INTEGRAL = "integral";
	public static final String AMOUNT = "amount";
	public static final String COUNTRY = "country";
	public static final String CURRENCY = "currency";
	public static final String PLATFORM = "platform";
	public static final String ORDER_NO = "order_no";
	public static final String THIRD_ORDER_NO = "third_order_no";
	public static final String REFUND_AMOUNT = "refund_amount";
	public static final String FEE = "fee";
	public static final String REAL_AMOUNT = "real_amount";
	public static final String BAD_DEBT = "bad_debt";
	public static final String VERIFY_USER_ID = "verify_user_id";
	public static final String REVIEW_USER_ID = "review_user_id";
	public static final String VERIFY_TIME = "verify_time";
	public static final String REVIEW_TIME = "review_time";
	public static final String VERIFY_REMARK = "verify_remark";
	public static final String REVIEW_REMARK = "review_remark";
	public static final String REMARK = "remark";
	public static final String STATUS = "status";
	public static final String PAY_RES_INFO = "pay_res_info";
	public static final String PAYMENT_RESULT = "payment_result";

	public boolean canVerify() {
		return CashStatus.INIT.eq(status);
	}

	public boolean canReview() {
		return CashStatus.VERIFY_TRUE.eq(status);
	}

	public boolean canCheck() {
		return CashStatus.OK.eq(status) && Status.NO.eq(checkStatus);
	}

	public PayPlatform getPlatformEnum() {
		return PayPlatform.of(platform);
	}

	public String getPlatformName() {
		if (getPlatformEnum().value == 5) {
			return "";
		}
		return X.map(getPlatformEnum(), PayPlatform::name);
	}

	public String getCurrency_() {
		return X.map(Currency.of(currency), Currency::getLabel);
	}

	public String getStatus_() {
		return CashStatus.of(this.status).getLabel();
	}

	public BigDecimal calcRealAmount() {
		return amount.subtract(fee).subtract(badDebt);
	}

	public Map<String, Object> packEmailContent(String username, Date now) {
		return CollectionUtil.ofHashMap("username", username,
				"userId", Member.encode(userId),
				"cashMoney", Formats.formatNumber_M(realAmount),
				"cashFee", Formats.formatNumber_M(fee),
				"cashAddTime", Formats.formatDate_D(addTime),
				"cashArriveTime", Formats.formatDate_D(now),
				"cashOrderNO", orderNo);
	}

	@Override
	public void setStatus(Integer status) {
		this.status = status;
	}

}
